<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="主机序号" prop="hostId">
        <el-input
            v-model="queryParams.hostId"
            placeholder="请输入主机序号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主机编号" prop="hostNo">
        <el-input
            v-model="queryParams.hostNo"
            placeholder="请输入主机编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主机名称" prop="hostName">
        <el-input
            v-model="queryParams.hostName"
            placeholder="请输入主机名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="是否有效" prop="valid">
        <el-select v-model="queryParams.valid" placeholder="请选择是否有效" clearable>
          <el-option
              v-for="dict in valid"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['meas:hostInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['meas:hostInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['meas:hostInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['meas:hostInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hostInfoList" @selection-change="handleSelectionChange" stripe >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水号" align="center" prop="id" v-if="false"/>
      <el-table-column label="主机序号" align="center" prop="hostId" />
      <el-table-column label="主机编号" align="center" prop="hostNo" />
      <el-table-column label="主机名称" align="center" prop="hostName" width="110"/>
      <el-table-column label="主机ip" align="center" prop="hostIp" width="110"/>
      <el-table-column label="主机端口" align="center" prop="hostPort" width="90"/>
      <el-table-column label="GPS经度" align="center" prop="lo" width="130"/>
      <el-table-column label="GPS纬度" align="center" prop="la" width="130"/>
      <el-table-column label="地图经度" align="center" prop="mapLo" width="130"/>
      <el-table-column label="地图纬度" align="center" prop="mapLa" width="130"/>
      <el-table-column label="主机启用日期" align="center" prop="enabledTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.enabledTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="130"/>
      <el-table-column label="是否有效" align="center" prop="valid">
        <template #default="scope">
          <dict-tag :options="valid" :value="scope.row.valid"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding " width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['meas:hostInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['meas:hostInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改检测设备主机信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="hostInfoRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="主机序号" prop="hostId">
          <el-input v-model="form.hostId" placeholder="请输入主机序号" />
        </el-form-item>
        <el-form-item label="主机编号" prop="hostNo">
          <el-input v-model="form.hostNo" placeholder="请输入主机编号" />
        </el-form-item>
        <el-form-item label="主机名称" prop="hostName">
          <el-input v-model="form.hostName" placeholder="请输入主机名称" />
        </el-form-item>
        <el-form-item label="主机ip" prop="hostIp">
          <el-input v-model="form.hostIp" placeholder="请输入主机ip" />
        </el-form-item>
        <el-form-item label="主机端口" prop="hostPort">
          <el-input v-model="form.hostPort" placeholder="请输入主机端口" />
        </el-form-item>
        <el-form-item label="GPS经度" prop="lo">
          <el-input v-model="form.lo" placeholder="请输入GPS经度" />
        </el-form-item>
        <el-form-item label="GPS纬度" prop="la">
          <el-input v-model="form.la" placeholder="请输入GPS纬度" />
        </el-form-item>
        <el-form-item label="地图经度" prop="mapLo">
          <el-input v-model="form.mapLo" placeholder="请输入地图经度" />
        </el-form-item>
        <el-form-item label="地图纬度" prop="mapLa">
          <el-input v-model="form.mapLa" placeholder="请输入地图纬度" />
        </el-form-item>
        <el-form-item label="主机启用日期" prop="enabledTime">
          <el-date-picker clearable
                          v-model="form.enabledTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择主机启用日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否有效" prop="valid">
          <el-select v-model="form.valid" placeholder="请选择是否有效">
            <el-option
                v-for="dict in valid"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="HostInfo">
import { listHostInfo, getHostInfo, delHostInfo, addHostInfo, updateHostInfo } from "@/api/meas/hostInfo";

const { proxy } = getCurrentInstance();
const { valid } = proxy.useDict('valid');

const hostInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hostId: null,
    hostNo: null,
    hostName: null,
    valid: null,
  },
  rules: {
    hostId: [
      { required: true, message: "主机序号不能为空", trigger: "blur" }
    ],
    hostNo: [
      { required: true, message: "主机编号不能为空", trigger: "blur" }
    ],
    hostName: [
      { required: true, message: "主机名称不能为空", trigger: "blur" }
    ],
    hostIp: [
      { required: true, message: "主机ip不能为空", trigger: "blur" }
    ],
    hostPort: [
      { required: true, message: "主机端口不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询检测设备主机信息列表 */
function getList() {
  loading.value = true;
  listHostInfo(queryParams.value).then(response => {
    hostInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    hostId: null,
    hostNo: null,
    hostName: null,
    hostIp: null,
    hostPort: null,
    lo: null,
    la: null,
    mapLo: null,
    mapLa: null,
    enabledTime: null,
    remark: null,
    valid: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("hostInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加检测设备主机信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getHostInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改检测设备主机信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hostInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateHostInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addHostInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除检测设备主机信息编号为"' + _ids + '"的数据项？').then(function() {
    return delHostInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('meas/hostInfo/export', {
    ...queryParams.value
  }, `hostInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
